package com.labweb.servicos;

import java.util.List;

import javax.persistence.RollbackException;
import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.SystemException;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;

import com.labweb.entidades.Laboratorio;
import com.labweb.util.HibernateUtil;

public class LaboratorioService {
	public Laboratorio pesquisarPorId(Integer numero) {
		Session session =  HibernateUtil.getSession();
		try {
			return (Laboratorio) session.get(Laboratorio.class, numero);
		} finally {
			session.close();
		}
	}
	
	public void salvar(Laboratorio laboratorio) throws Exception, SecurityException, RollbackException, HeuristicMixedException, HeuristicRollbackException, SystemException {
		Session session = (Session) HibernateUtil.getSession();
		Transaction tx = (Transaction) session.beginTransaction();
		session.saveOrUpdate(laboratorio);
		tx.commit();
		session.close();
	}

	
	public List<Laboratorio> listarTodas() {
		Session session =  HibernateUtil.getSession();
		try {
			return session.createCriteria(Laboratorio.class).addOrder(Order.asc("numero")).list();
		} finally {
			session.close();
		}
	}
	
	
	public void excluir(Laboratorio laboratorio) throws Exception, SecurityException, RollbackException, HeuristicMixedException, HeuristicRollbackException, SystemException {
		// TODO Auto-generated method stub
		 
			Session session = (Session) HibernateUtil.getSession();
			Transaction tx = (Transaction) session.beginTransaction();
			session.delete(laboratorio);
			tx.commit();
			session.close();

		
	}
}
